package com.cee.controller;

import com.cee.common.result.Result;
import com.cee.common.result.ResultCodeEnum;
import com.cee.dto.AddExamSubjectDTO;
import com.cee.exception.CeeException;
import com.cee.model.ExamManySelect;
import com.cee.model.ExamOneSelect;
import com.cee.service.ExamManySelectService;
import com.cee.service.ExamOneSelectService;
import com.cee.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/examManySelect")
public class ExamManySelectController {

    @Autowired
    private ExamManySelectService examManySelectService;

    @Autowired
    private ExamService examService;


    @GetMapping("/delete")
    public Result deleteExamManySelect(Integer id, Integer ceeExamId){
        examManySelectService.deleteExamManySelectById(id);

        // 更新总分
        examService.countGradeAndUpdate(ceeExamId);
        return Result.ok();
    }


    @PostMapping("/update")
    public Result updateExamManySelect(@RequestBody ExamManySelect examManySelect){
        examManySelectService.updateExamManySelect(examManySelect);

        // 更新总分
        examService.countGradeAndUpdate(examManySelect.getCeeExamId());
        return Result.ok();
    }

    @PostMapping("/add")
    public Result saveExamManySelect(@RequestBody AddExamSubjectDTO addExamSubjectDTO){

        if (addExamSubjectDTO.getList()==null || addExamSubjectDTO.getExamId() == null
                || addExamSubjectDTO.getGrade() == null){
            throw new CeeException(ResultCodeEnum.PARAM_ERROR);
        }

        for (Integer id : addExamSubjectDTO.getList()){
            ExamManySelect examManySelect = new ExamManySelect();
            examManySelect.setCeeManySelectId(id);
            examManySelect.setCeeExamId(addExamSubjectDTO.getExamId());
            examManySelect.setGrade(addExamSubjectDTO.getGrade());

            examManySelectService.saveExamManySelect(examManySelect);
        }

        examService.countGradeAndUpdate(addExamSubjectDTO.getExamId());

        return Result.ok();
    }

}
